package com.artemis.event;

import java.util.HashMap;

import com.artemis.World;

/**
 * The base class for all system-events.
 * 
 * a system-event can be posted and polled via an {@link EventDeliverySystem} 
 * or the {@link World}.
 * 
 * @author kryo
 *
 */
public abstract class SystemEvent {
	private final int typeIndex;
	
	/**
	 * Set this to true to mark the event as handled.
	 * If an event is marked as handled, the {@link EventDeliverySystem} will 
	 * eat the event so it is not passed on to the system under the current system.
	 */
	public boolean handled;
	
	public SystemEvent() {
		typeIndex = EventIndexManager.getIndexFor(this.getClass());
	}
	
	/**
	 * Get the type-index of the event.
	 * 
	 * @return the type-index.
	 * 
	 * @see SystemEvent.EventIndexManager#getIndexFor(Class)
	 */
	public int getTypeId() {
		return typeIndex;
	}
	
	/**
	 * Used to generate a unique bit for each event-type.
	 */
	public static class EventIndexManager {
		private static int INDEX = 0;
		private static HashMap<Class<? extends SystemEvent>, Integer> indices = new HashMap<Class<? extends SystemEvent>, Integer>();
		
		public static int getIndexFor(Class<? extends SystemEvent> eventType){
			Integer index = indices.get(eventType);
			if(index == null) {
				index = INDEX++;
				indices.put(eventType, index);
			}
			return index;
		}
	}
}
